package cn.woochen.functionwidget

import android.app.Activity
import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.util.Log
import cn.woochen.functionwidget.sample.SdkActivity
import cn.woochen.functionwidget.sample.util.PackageUtil
import cn.woochen.functionwidget.util.SimpleActivityLifecycleCallbacks
import cn.woochen.functionwidget.widget.FloatView


class AppApplication : Application() {
    var networkConnectChangedReceiver: NetworkConnectChangedReceiver? = null

    private var activityCount: Int = 0

    override fun onCreate() {
        super.onCreate()
        if (BuildConfig.DEBUG) {
// 根据wifi切换运行环境
//  registerWifiBroadCast()
            //手动切换运行环境
            /* registerActivityLifecycleCallbacks(object : SimpleActivityLifecycleCallbacks() {

                 override fun onActivityStarted(activity: Activity?) {
                     activityCount++
                     FloatView.createView(this@AppApplication)
                 }

                 override fun onActivityStopped(activity: Activity?) {
                     activityCount--
                     if (activityCount == 0) FloatView.dismiss()
                 }

             })*/
        }

    }


    /**
     * 根据wifi环境切换开发环境
     */
    private fun registerWifiBroadCast() {
        Log.e("eee", "注册广播")
        val filter = IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
        networkConnectChangedReceiver = NetworkConnectChangedReceiver()
        registerReceiver(networkConnectChangedReceiver, filter)
    }


    class NetworkConnectChangedReceiver : BroadcastReceiver() {
        var preEnvironmentState = -1 // 0->测试环境 1->正式环境

        override fun onReceive(context: Context?, intent: Intent?) {
            val networkInfo = intent?.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) as NetworkInfo?
            Log.e("eee", "wifi是否连接${networkInfo?.isConnected}")
            if (networkInfo != null && networkInfo.isConnected) {
                val wm = context?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager?
                val wifiInfo = wm?.connectionInfo
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Log.e("eee", wifiInfo?.frequency.toString())
                    if (wifiInfo?.frequency.toString().startsWith("2")) {
                        Log.e("eeeeeeeeee", "${wifiInfo?.frequency.toString()}您进入测试环境")
                        reLogin(0)
                    } else if (wifiInfo?.frequency.toString().startsWith("5")) {
                        Log.e("eeeeeeeeee", "${wifiInfo?.frequency.toString()}您进入正式环境")
                        reLogin(1)
                    }
                }
            }
        }

        /**
         * 重新登录
         */
        private fun reLogin(environmentState: Int) {
            Log.e("eee", "之前环境->$preEnvironmentState  当前环境->$environmentState")
            if (preEnvironmentState != environmentState) {
                val currentActivity = PackageUtil.currentActivity
                if (currentActivity !is SdkActivity) {
                    val intent = Intent(currentActivity, SdkActivity::class.java)
                    currentActivity?.startActivity(intent)
                }
                preEnvironmentState = environmentState
            }
        }

    }


}